﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.Model;
using dhSystemBLL.basicData;

namespace dhsystem.basicForm
{
    public partial class FrmModuleaccessAdd : Form
    {
        modulAaccessBLL modulAccess = new modulAaccessBLL();
        modulAaccessModel modulAccessRow = new modulAaccessModel();
        moduleListBLL moduleList = new moduleListBLL();
        accessGroupBLL accessGroup = new accessGroupBLL(); 
        BindingSource b1;
        string state;
        string groupnum;
        string moduleNum;
        //绑定数据源
        public FrmModuleaccessAdd(BindingSource b2, string _state, string _groupnum, string _moduleNum)
        {
            InitializeComponent();
            state = _state;
            b1 = b2;
            groupnum = _groupnum;
            moduleNum = _moduleNum;
        }
        //绑定数据源
        public FrmModuleaccessAdd(BindingSource b2, string _state)
        {
            InitializeComponent();
            state = _state;
            b1 = b2;
        }
        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }
        //绑定数据源
        private void FrmModuleaccessAdd_Load(object sender, EventArgs e)
        {
            comboBoxgroupNum.Focus();
            bindingSourceaccessGroup.DataSource = accessGroup.getaccessGroupAll();
            comboBoxgroupNum.DataSource = bindingSourceaccessGroup;
            comboBoxgroupNum.DisplayMember = "groupName";
            comboBoxgroupNum.ValueMember = "groupNum";
            bindingSourcemoduleList.DataSource = moduleList.getmoduleListAll();
            comboBoxmoduleNum.DataSource = bindingSourcemoduleList;
            comboBoxmoduleNum.DisplayMember = "moduleName";
            comboBoxmoduleNum.ValueMember = "moduleNum";
            if (state == "编辑")
            {
                comboBoxmoduleNum.DataBindings.Add("text", b1, "moduleNum");
                textrecordid.DataBindings.Add("Text", b1, "recordid");
                comboBoxgroupNum.DataBindings.Add("text", b1, "groupNum");
                checkshowAc.DataBindings.Add("checked", b1, "showAc");
                checkaddAc.DataBindings.Add("checked", b1, "addAc");
                checkeditAc.DataBindings.Add("checked", b1, "editAc");
                checkdelAc.DataBindings.Add("checked", b1, "delAc");
                checkexportAc.DataBindings.Add("checked", b1, "exportAc");
                checkprintAc.DataBindings.Add("checked", b1, "printAc");
                checkimportAc.DataBindings.Add("checked", b1, "importAc");
                textUpdateUser.DataBindings.Add("text", b1, "updateUser");
                textCreateUser.DataBindings.Add("text", b1, "createUser");
                textmark.DataBindings.Add("text", b1, "mark");
                comboBoxgroupNum.SelectedValue = groupnum;
                comboBoxmoduleNum.SelectedValue = moduleNum;
            }
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (state == "新增")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否新增模块表格！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        modulAccessRow .recordid  = Convert.ToInt32(textrecordid.Text);
                        modulAccessRow .moduleNum   = comboBoxmoduleNum.SelectedValue.ToString();
                        modulAccessRow .groupNum  = comboBoxgroupNum.SelectedValue.ToString();
                        modulAccessRow.showAc = checkshowAc.Checked;
                        modulAccessRow.editAc = checkeditAc.Checked;
                        modulAccessRow.addAc = checkaddAc.Checked;
                        modulAccessRow.delAc = checkdelAc.Checked;
                        modulAccessRow.exportAc = checkexportAc.Checked;
                        modulAccessRow.printAc = checkprintAc.Checked;
                        modulAccessRow.importAc = checkimportAc.Checked;
                        modulAccessRow.createDate = DateTime.Now;
                        modulAccessRow.updateDate = DateTime.Now;
                        modulAccessRow.createUser = textCreateUser.Text;
                        modulAccessRow.updateUser = textUpdateUser.Text;
                        modulAccessRow.mark = textmark.Text;
                        modulAccess.modulAaccessInsert(modulAccessRow);
                        b1.DataSource = modulAccess.getmodulAaccessAll();
                        MessageBox.Show("新增成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
            if (state == "编辑")
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否修改模块表格！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        modulAccessRow.recordid = Convert.ToInt32(textrecordid.Text);
                        modulAccessRow.moduleNum = comboBoxmoduleNum.SelectedValue.ToString();
                        modulAccessRow.groupNum = comboBoxgroupNum.SelectedValue.ToString();
                        modulAccessRow.showAc = checkshowAc.Checked;
                        modulAccessRow.editAc = checkeditAc.Checked;
                        modulAccessRow.addAc = checkaddAc.Checked;
                        modulAccessRow.delAc = checkdelAc.Checked;
                        modulAccessRow.exportAc = checkexportAc.Checked;
                        modulAccessRow.printAc = checkprintAc.Checked;
                        modulAccessRow.importAc = checkimportAc.Checked;
                        modulAccessRow.createDate = DateTime.Now;
                        modulAccessRow.updateDate = DateTime.Now;
                        modulAccessRow.createUser = textCreateUser.Text;
                        modulAccessRow.updateUser = textUpdateUser.Text;
                        modulAccessRow.mark = textmark.Text;
                        modulAccess.modulAaccessUpdate(modulAccessRow);
                        b1.DataSource = modulAccess.getmodulAaccessAll();
                        MessageBox.Show("修改成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
